import datetime
from django.db import models


class Expert(models.Model):

    # 专家的性别
    name = models.CharField(
        max_length=60, null=False, verbose_name="姓名", db_comment="姓名"
    )

    # 专家的职称
    title = models.CharField(
        max_length=60, null=True, verbose_name="职称", db_comment="职称"
    )
    # 专家的工作单位
    work_unit = models.CharField(
        max_length=60, null=True, verbose_name="工作单位", db_comment="工作单位"
    )
    # 专家的专业领域
    specialty = models.CharField(
        max_length=120,
        null=True,
        blank=True,
        db_comment="专业领域",
        verbose_name="专业领域",
    )
    # 专家的手机号
    phone_number = models.CharField(
        blank=True,
        default="",
        max_length=20,
        db_comment="手机号码",
        verbose_name="手机号码",
    )

    # 专家的联系方式
    contact_information = models.TextField(
        blank=True,
        default="",
        verbose_name="其他联系方式",
        help_text="其他联系方式",
        db_comment="其他联系方式",
    )
    # 专家的简介
    introduction = models.TextField(
        blank=True, default="", db_comment="简介", verbose_name="简介"
    )

    # 开户行
    bank_name = models.CharField(
        max_length=60, null=True, blank=True, db_comment="开户行", verbose_name="开户行"
    )

    # 开户行号
    bank_number = models.CharField(
        max_length=12,
        null=True,
        blank=True,
        db_comment="开户行号",
        verbose_name="开户行号",
    )
    # 银行卡号
    bank_account = models.CharField(
        max_length=60,
        null=True,
        blank=True,
        db_comment="银行卡号",
        verbose_name="银行卡号",
    )
    # 身份证号
    id_card_number = models.CharField(
        max_length=6,
        null=True,
        blank=True,
        db_comment="身份证号",
        verbose_name="身份证号",
    )
    # 是否为外单位专家
    is_out_unit_expert = models.BooleanField(
        default=True, verbose_name="是否为外单位专家", db_comment="是否为外单位专家"
    )

    # 是否已退休
    is_retired = models.BooleanField(
        default=False, db_comment="是否已退休", verbose_name="是否已退休"
    )
    # 机动车拍照号码
    license_plate_number = models.CharField(
        max_length=60,
        null=True,
        blank=True,
        db_comment="机动车拍照号码",
        verbose_name="车号",
    )

    SHIFT_GENDER = (
        (0, "未知的性别"),
        (1, "男"),
        (2, "女"),
        (9, "未说明的性别"),
    )
    # 专家的性别
    gender = models.SmallIntegerField(
        default=1, choices=SHIFT_GENDER, db_comment="性别", verbose_name="性别"
    )

    # 专家的单位
    unit = models.ForeignKey(
        "Unit",
        on_delete=models.SET_NULL,
        null=True,
        db_comment="单位",
        verbose_name="单位",
    )

    # 专业领域
    expertise_areas = models.ManyToManyField("ExpertiseArea")

    # 专家的出生日期
    birthday = models.DateField(
        null=True,
        blank=True,
        verbose_name="出生日期",
        db_comment="出生日期",
        help_text="请输入格式为YYYY-MM-DD的日期。",
    )

    # 专家的年龄
    @property
    def age(self):
        return (datetime.datetime.now() - self.birthday).days // 365

    class Meta:
        db_table_comment = "专家信息表"
        verbose_name = "专家信息"
        verbose_name_plural = "专家信息表"

    def __str__(self):
        return self.name


class Unit(models.Model):
    name = models.CharField(max_length=255, verbose_name="单位名称", db_comment="单位")
    # 排序
    sort = models.IntegerField(default=999, verbose_name="排序", db_comment="排序")

    class Meta:
        db_table_comment = "单位信息表"
        verbose_name = "单位信息"
        verbose_name_plural = "单位信息表"
        ordering = ["sort"]

    def __str__(self):
        return f"{self.name}"


class ExpertiseArea(models.Model):

    name = models.CharField(max_length=100, verbose_name="领域名称", db_comment="领域")

    class Meta:
        db_table_comment = "专家领域信息表"
        verbose_name = "领域信息"
        verbose_name_plural = "专家领域信息表"

    def __str__(self):
        return f"{self.name}"


# ... existing code...
